package com.dw.widget.slideshow.utils;

import android.graphics.Bitmap;
import com.dw.widget.slideshow.utils.log.Log;

/**
 * 图片片段循环队列
 * @author daiyu
 *
 */
public class ClipQueue {

	private static ClipQueue _instance;
	private int length = 8;
	private ImageClip[] clipQueue;
	private int en_index = 0;     // 入列指针
	private int de_index = 0;     // 出列指针
	private int count = 0;        // 剩余数
	
	private final String TAG = "ClipQueue";
	
	public ClipQueue(int len)
	{
		if(len > 0)
		{
			length = len;
		}
		
		clipQueue = new ImageClip[length];
	}
	
	public static ClipQueue getInstance(int len)
	{
		if(_instance == null)
		{
			_instance = new ClipQueue(len);
		}
		
		return _instance;
	}
	
	public static ClipQueue getInstance()
	{
		if(_instance == null)
		{
			_instance = new ClipQueue(8);
		}
		
		return _instance;
	}
	
	/**
	 * 入队列
	 */
	public void enQueue(ImageClip clip)
	{
		if(clip == null || count >= length)
		{
			return;
		}
		
		en_index = (en_index) % length;
		Log.d(null, TAG, String.format("enQueue en_index=%1$d, de_index=%2$d, count=%3$d", en_index, de_index, count));
		
		clipQueue[en_index] = clip;
		en_index++;
		
		count++;
	}
	
	/**
	 * 入队列
	 * @param bmps
	 */
	public void enQueue(Bitmap[] bmps, String orientation)
	{
		if(bmps == null)
		{
			return;
		}
		
		for(int i = 0; i < bmps.length; i++)
		{
			ImageClip clip = getInstance().new ImageClip();
			clip.setImage(bmps[i]);
			clip.setLastClip(i >= bmps.length - 1);
			clip.setOrientation(orientation);
			
			enQueue(clip);
		}
	}
	
	/**
	 * 出队列
	 */
	public ImageClip deQueue()
	{
		if(count <= 0)
		{
			return null;
		}
		
		de_index = (de_index) % length;
		Log.d(null, TAG, String.format("deQueue en_index=%1$d, de_index=%2$d, count=%3$d", en_index, de_index, count));
		
		ImageClip clip = clipQueue[de_index];
		de_index++;
		
		count--;
		
		return clip;
	}
	
	/**
	 * 获取下一个图片Clip
	 * @return
	 */
	public ImageClip nextClip()
	{
		if(count <= 0)
		{
			return null;
		}
		
		de_index = (de_index) % length;
		ImageClip clip = clipQueue[de_index];
		
		return clip;
	}
	
	public ImageClip previousClip(int step)
	{
		ImageClip clip = clipQueue[(de_index - step) % length];	
		return clip;
	}
	
	/**
	 * 获取Clip数量
	 * @return
	 */
	public int getCount()
	{
		return count;
	}
	
	public void reset()
	{
		en_index = 0;
		de_index = 0;
		count = 0;
	}

	public class ImageClip{
		
		private Bitmap image;
		private String orientation;
		private boolean isLastClip;
		
		public final Bitmap getImage() {
			return image;
		}
		public final void setImage(Bitmap image) {
			this.image = image;
		}
		public final boolean isLastClip() {
			return isLastClip;
		}
		public final void setLastClip(boolean isLastClip) {
			this.isLastClip = isLastClip;
		}
		public final String getOrientation() {
			return orientation;
		}
		public final void setOrientation(String orientation) {
			this.orientation = orientation;
		}
		
	}
}